<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

if( isset( $_SESSION[ 'ruler_id' ] ) ) {
	$query = "SELECT turn, turns_inactive FROM game";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$current_turn = $row[ 'turn' ];
	$turns_to_inactive = $row[ 'turns_inactive' ];;
	//include_once( "../global/old_database_connection.php" );
	//include( "../global/utils.php" );
	$x = formatInput( $_GET[ 'x' ] );
	$y = formatInput( $_GET[ 'y' ] );

	if( is_numeric( $x ) && is_numeric( $y ) ) {
		$query = "SELECT faction_id " .
				"FROM faction_member " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'faction_id' ] ) ) {
			$faction_id = $row[ 'faction_id' ];
			$query = "SELECT village.village_id " .
					"FROM scouted " .
					"JOIN village ON scouted.village_id = village.village_id " .
					"WHERE x = $x AND y = $y " .
						"AND scouted.ruler_id IN ( SELECT DISTINCT ruler_id FROM faction_member WHERE faction_id = " . $row[ 'faction_id' ] . " AND requested = 0 AND invited = 0 )";
			$ruler_id_in = " IN ( SELECT DISTINCT ruler_id FROM faction_member WHERE faction_id = " . $row[ 'faction_id' ] . " AND requested = 0 AND invited = 0 )";
		}
		else {
			$query = "SELECT village.village_id " .
					"FROM scouted " .
					"JOIN village ON scouted.village_id = village.village_id " .
					"WHERE scouted.ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND x = $x AND y = $y";
			$ruler_id_in = " = " . $_SESSION[ 'ruler_id' ];
		}
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );

		if( isset( $row[ 'village_id' ] ) ) {
			$query2 = "SELECT start_turn FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

			$village_id = $row[ 'village_id' ];
			$query = "SELECT *" .
					"FROM village " .
					"WHERE village_id = " . $row[ 'village_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			
			$query3 = "SELECT name " .
					"FROM village_types " .
					"WHERE village_type_id = " . $row[ 'village_type_id' ];
			$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
			$location_title = translate( $row3[ 'name' ] );
			
			$range = "";
			if( $row[ 'add_turn' ] + 800 > $current_turn && $row2[ 'start_turn' ] <= $row[ 'add_turn' ] - 200 ) {
				$range = " (" . translate( 'currently unavailable' ) . ")";
			}
			elseif( !checkRangeVillage( $row[ 'village_id' ] ) ) {
				$range = " (" . translate( 'out of range' ) . ")";
			}

			if( $row[ 'ruler_id' ] != -1 ) {
				$query2 = "SELECT name, active_turn, resigned " .
						"FROM ruler " .
						//"JOIN users ON ruler.user_id = users.user_id " .
						"WHERE ruler_id = " . $row[ 'ruler_id' ];
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$query3 = "SELECT banned " .
						"FROM ruler " .
						"JOIN users ON ruler.user_id = users.user_id " .
						"WHERE ruler_id = " . $row[ 'ruler_id' ];
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( $row3[ 'banned' ] == 1 ) {
					$inactive = " (" . translate( 'permanently banned' ) . ")";
				}
				elseif( $row2[ 'resigned' ] == 1 ) {
					$inactive = " (" . translate( 'resigned' ) . ")";
				}
				elseif( $row2[ 'active_turn' ] + $turns_to_inactive < $current_turn ) {
					$inactive = " (" . translate( 'inactive' ) . ")";
				}
				else {
					$inactive = "";
				}
				$ruler_name = translate( 'Owner' ) . ": <a href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>$inactive";
			}
			else {
				$ruler_name = "";
			}

//			$query2 = "SELECT start_place, ruler_id FROM village WHERE village_id = " . $row[ 'village_id' ];
//			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
//			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

			if( $row[ 'start_place' ] == 1 ) {
				$title = "Capital";
			}
			else {
				if( $row[ 'population' ] < 5000 ) {
					$title = "Outpost";
				}
				else if( $row[ 'population' ] < 10000 ) {
					$title = "Settlement";
				}
				else if( $row[ 'population' ] < 50000 ) {
					$title = "Village";
				}
				else if( $row[ 'population' ] < 150000 ) {
					$title = "Town";
				}
				else if( $row[ 'population' ] < 500000 ) {
					$title = "City";
				}
				else {
					$title = "Metropolis";
				}
			}

			$population = translate( "Population" ) . ": <strong class=\"population\">" . number_format( $row[ 'population' ] ) . '</strong>';

			if( $_SESSION[ 'ruler_id' ] == $row[ 'ruler_id' ] ) {
				$name = translate( $title ) . ": <a href=\"../villages/village.php?name=" . $row[ 'name' ] . "\">" . $row[ 'name' ] . "</a>";
			}
			else if( $row[ 'name' ] == "" ) {
				$name = translate( 'Unnamed' ) . " " . strtolower( translate( $title ) );
			}
			else {
				$name = translate( $title ) . ": " . $row[ 'name' ];
			}

			$query2 = "SELECT tag, name " .
					"FROM faction " .
					"JOIN faction_member ON faction.faction_id = faction_member.faction_id " .
					"WHERE ruler_id = " . $row[ 'ruler_id' ] . " AND requested = 0 AND invited = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			if( isset( $row2[ 'name' ] ) ) {
				$faction = ", <a href=\"../overview/faction.php?name=" . $row2[ 'name' ] . "\">[" . strtoupper( $row2[ 'tag' ] ) . "]</a>";
			}
			else {
				$faction = "";
			}

			$text = $name . "$range<br />\r\n" .
				   translate( "Location" ) . ": ($x, $y)<br />\r\n" .
				   $population . "<br />\r\n" .
				   $ruler_name  . $faction . "<br />\r\n";
				   
			if( $row[ 'food_income' ] > 0 ) {
				$text .= translate( "Food income" ) . ": <strong class=\"food\">" . number_format( $row[ 'food_income' ] ) . "</strong>, quality: <strong class=\"food\">" . $row[ 'food_quality' ] . "</strong><br />\r\n";
			}
			else {
				$text .= translate( "Food quality" ) . ": <strong class=\"food\">" . $row[ 'food_quality' ] . "</strong><br />\r\n";
			}
			
			if( $row[ 'iron_income' ] > 0 ) {
				$text .= translate( "Iron income" ) . ": <strong class=\"iron\">" . number_format( $row[ 'iron_income' ] ) . "</strong>, quality: <strong class=\"iron\">" . $row[ 'iron_quality' ] . "</strong><br />\r\n";
			}
			else {
				$text .= translate( "Iron quality" ) . ": <strong class=\"iron\">" . $row[ 'iron_quality' ] . "</strong><br />\r\n";
			}
			
			if( $row[ 'coal_income' ] > 0 ) {
				$text .= translate( "Coal income" ) . ": <strong class=\"coal\">" . number_format( $row[ 'coal_income' ] ) . "</strong>, quality: <strong class=\"coal\">" . $row[ 'coal_quality' ] . "</strong><br />\r\n";
			}
			else {
				$text .= translate( "Coal quality" ) . ": <strong class=\"coal\">" . $row[ 'coal_quality' ] . "</strong><br />\r\n";
			}
			
			$text .= translate( "Free space" ) . ": <strong class=\"space-available\">" . $row[ 'space' ] . '</strong>';
			
			$query2 = "SELECT name, village_buildings.building_id " .
					"FROM village_buildings " .
					"JOIN buildings ON village_buildings.building_id = buildings.building_id " .
					"WHERE village_id = $village_id AND map_visible = 1 " .
					"ORDER BY food_income DESC, coal_income DESC, iron_income DESC, space DESC, name";
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$num2 = mysql_numrows($result2);
			$i2 = 0;
			if( $num2 > 0 ) {
				$text .= '<br />' . translate( 'Notable buildings' ) . ': ';
				while ($i2 < $num2) {
					if( $i2 == $num2 -1 && $i2 > 0 ) {
						$text .= ' ' . translate( 'and' ) . ' ';
					}
					elseif( $i2 > 0 ) {
						$text .= ', ';
					}
					$tooltip = getBuildingTooltip( mysql_result( $result2, $i2, 'building_id' ) );
					$text .= "<a href=\"../villages/building.php?id=" . mysql_result( $result2, $i2, 'building_id' ) . "\" title=\"$tooltip\" class=\"make-yui-tooltip\">" . translate( mysql_result($result2, $i2, 'name') ) . '</a>';
					$i2 ++;
				}
			}
			
			if( $_SESSION[ 'ruler_id' ] == $row[ 'loyal_to' ] ) {
				$loyal_towards = '<strong class="green">';
			}
			else {
				$allied = false;
				$query2 = "SELECT faction_id " .
						"FROM faction_member " .
						"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
				if( isset( $row2[ 'faction_id' ] ) ) {
					$faction_id = $row2[ 'faction_id' ];
					$query2 = "SELECT faction_id " .
							"FROM faction_member " .
							"WHERE ruler_id = " . $row[ 'loyal_to' ] . " AND invited = 0 AND requested = 0";
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
					if( isset( $row2[ 'faction_id' ] ) && $row2[ 'faction_id' ] == $faction_id ) {
						$allied = true;
					}
				}
				
				if( $allied ) {
					$loyal_towards = '<strong class="blue">';
				}
				else {
					$loyal_towards = '<strong class="red">';
				}
			}
			
			$query2 = "SELECT name " .
					"FROM ruler " .
					"WHERE ruler_id = " . $row[ 'loyal_to' ];
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			if( isset( $row2[ 'name' ] ) ) {
				$loyal_towards = '<a href="../overview/ruler.php?name=' . $row2[ 'name' ] . '">' . $loyal_towards . $row2[ 'name' ] . '</strong></a>';
			}
			else {
				$loyal_towards = '<strong class="brown">' . translate( 'Independents' ) . '</strong>';
			}
			$text .= '<br />' . $row[ 'loyalty' ] . '% ' . translate( 'loyalty towards' ) . ' ' . $loyal_towards . '<br />';			
			
			if( $row[ 'ruler_id' ] == -1 ) {
				$text .= translate( 'Known as' ) . ": $location_title<br />\r\n";
			}
			else {
				$text .= "<br />\r\n";
			}
		}
		else {
			$text = translate( "Location" ) . ": ($x, $y)<br />\r\n";
		}
		
		// Stationary armies
		$totals = Array();
		$colors = Array( 'Independent' => "<strong class=\"red\">" );
		$count = Array();
		
		$query2 = "SELECT DISTINCT name, army.ruler_id, eta, scout, infantry, cavalry, artillery " .
				"FROM army " .
				"JOIN army_visible ON army_visible.army_id = army.army_id " .
				"WHERE x = " . $x . " AND y = " . $y . " AND travel = 0 " .
				"AND army_visible.ruler_id " . $ruler_id_in . 
				" AND ( scout + infantry + cavalry + artillery ) > 0 ORDER BY bombard DESC, name";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$num2 = mysql_numrows( $result2 );
		$p = 0;
		if( $num2 > 0 ) {
			$last = translate( "Armies" ) . ":<br />";
			while ( $p < $num2 ) {
				$faction_name = "";
				$army_name = "";
				$allied = false;
				$result3 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result2, $p, "ruler_id" ) . " AND requested = 0 AND invited = 0" ) or die ( "Query failed: " . mysql_error() );
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				if( mysql_result( $result2, $p, "ruler_id" ) == $_SESSION[ 'ruler_id' ] ) {
					$strong = "<strong class=\"green\">";
					$allied = true;
					$end = "</strong>";
					$army_name = "<a href=\"../armies/army.php?name=" . mysql_result( $result2, $p, "name" ) . "\">$strong" . mysql_result( $result2, $p, "name" ) . "$end</a>";
				}
				else {
					$result4 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0" ) or die ( "Query failed: " . mysql_error() );
					$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
					if( isset( $row3[ 'faction_id' ] ) && isset( $row4[ 'faction_id' ] ) && $row4[ 'faction_id' ] == $row3[ 'faction_id' ] ) {
						$strong = "<strong class=\"blue\">";
						$allied = true;
						$end = "</strong>";
					}
					else {
						$strong = "<strong class=\"red\">";
						$end = "</strong>";
					}
					$army_name = $strong . mysql_result( $result2, $p, "name" ) . $end;
				}
				
				if( isset( $row3[ 'faction_id' ] ) ) {
					$result4 = mysql_query( "SELECT tag, name " .
							"FROM faction " .
							"WHERE faction_id = " . $row3[ 'faction_id' ] ) or die ( "Query failed: " . mysql_error() );
					$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
					$faction_name = " - <a href=\"../overview/faction.php?name=" . $row4[ 'name' ] . "\">" . $strong . "[" . $row4[ 'tag' ] . "]$end</a>";
					if( $allied ) {
						$colors[ $row4[ 'name' ] ] = "<strong class=\"blue\">";
					}
					else {
						$colors[ $row4[ 'name' ] ] = "<strong class=\"red\">";
					}
					if( !isset( $totals[ $row4[ 'name' ] ] ) ) {
						$totals[ $row4[ 'name' ] ] = 0;
						$count[ $row4[ 'name' ] ] = 0;
					}
					$count[ $row4[ 'name' ] ] ++;
					$totals[ $row4[ 'name' ] ] += mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" );
				}
				else {
					if( !isset( $totals[ 'Independent' ] ) ) {
						$totals[ 'Independent' ] = 0;
						$count[ 'Independent' ] = 0;
					}
					$count[ 'Independent' ] ++;
					$totals[ 'Independent' ] += mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" );
				}
			    
			    if( mysql_result( $result2, $p, "ruler_id" ) != -1 ) {
				    $query3 = "SELECT name " .
				    		"FROM ruler " .
				    		"WHERE ruler_id = " . mysql_result( $result2, $p, "ruler_id" );
				    $result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				    $row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );		    	
					$ruler_name = " - <a href=\"../overview/ruler.php?name=" . $row3[ 'name' ] . "\">$strong" . $row3[ 'name' ] . "$end</a>";
			    }
			    else {
			    	$ruler_name = ' - ' . $strong . translate( 'Independent forces' ) . $end;
			    }
					
				$last .= $army_name . $ruler_name . $faction_name . " - " .
						$strong . number_format( mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" ) ) . 
						$end . "<br />";
				$p ++;
			}
			$last .= "<br />\r\n";
		}
		if( !isset( $last ) ) {
			$last = "";
		}
		
		if( count( $totals ) > 0 ) {
			//$last .= "<br />\n";
			$show = false;
			foreach( $count AS $check ) {
				if( $check > 1 ) {
					$show = true;
				}
			}
			if( $show ) {
				foreach( $totals AS $key => $value ) {
					if( $key == 'Independents' ) {
						$last .= $colors[$key] . translate( $key ) . ' ' . translate( 'total forces' ) . ": " . number_format( $value ) . "</strong><br />\n";
					}
					else {
						$last .= $colors[$key] . $key . ' ' . translate( 'total forces' ) . ": " . number_format( $value ) . "</strong><br />\n";
					}
				}
			}
		}
		
		$text .= $last;
		
		// Moving armies
		$last = null;
		$totals = Array();
		$count = Array();
		
		$query2 = "SELECT DISTINCT army.army_id, name, army.ruler_id, eta, scout, infantry, cavalry, artillery FROM army " .
				"JOIN army_visible ON army_visible.army_id = army.army_id " .
				"WHERE actual_x + 0.5 >= " . $x . " AND actual_x - 0.5 < " . $x . " AND actual_y + 0.5 >= " . $y . " AND actual_y - 0.5 < " . $y . " AND travel > 0 AND army_visible.ruler_id " . $ruler_id_in . " ORDER BY eta, actual_y, actual_x, name";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$num2 = mysql_numrows( $result2 );
		$p = 0;
		if( $num2 > 0 ) {
			$last = translate( "Armies moving through" ) . ": <br />\r\n";
			while ( $p < $num2 ) {
				$faction_name = "";
				$allied = false;
				$army_name = "";
				$result3 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result2, $p, "ruler_id" ) . " AND requested = 0 AND invited = 0" ) or die ( "Query failed: " . mysql_error() );
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				if( mysql_result( $result2, $p, "ruler_id" ) == $_SESSION[ 'ruler_id' ] ) {
					$strong = "<strong class=\"green\">";
					$end = "</strong>";
					$army_name = "<a href=\"../armies/army.php?name=" . mysql_result( $result2, $p, "name" ) . "\">$strong" . mysql_result( $result2, $p, "name" ) . "$end</a>";
					$allied = true;
				}
				elseif( mysql_result( $result2, $p, "ruler_id" ) == -1 ) {
					$strong = "<strong class=\"red\">";
					$end = "</strong>";
					$army_name = $strong . mysql_result( $result2, $p, "name" ) . $end;
				} 
				else {
					$result4 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0" ) or die ( "Query failed: " . mysql_error() );
					$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
					if( isset( $row3[ 'faction_id' ] ) && isset( $row4[ 'faction_id' ] ) && $row4[ 'faction_id' ] == $row3[ 'faction_id' ] ) {
						$strong = "<strong class=\"blue\">";
						$end = "</strong>";
						$allied = true;
					}
					else {
						$strong = "<strong class=\"red\">";
						$end = "</strong>";
					}
					$army_name = $strong . mysql_result( $result2, $p, "name" ) . $end;
				}
				
				if( isset( $row3[ 'faction_id' ] ) ) {
					$result4 = mysql_query( "SELECT tag, name " .
							"FROM faction " .
							"WHERE faction_id = " . $row3[ 'faction_id' ] ) or die ( "Query failed: " . mysql_error() );
					$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
					$faction_name = " - <a href=\"../overview/faction.php?name=" . $row4[ 'name' ] . "\">" . $strong . "[" . $row4[ 'tag' ] . "]$end</a>";
					if( $allied ) {
						$colors[ $row4[ 'name' ] ] = "<strong class=\"blue\">";
					}
					else {
						$colors[ $row4[ 'name' ] ] = "<strong class=\"red\">";
					}
					if( !isset( $totals[ $row4[ 'name' ] ] ) ) {
						$totals[ $row4[ 'name' ] ] = 0;
						$count[ $row4[ 'name' ] ] = 0;
					}
					$count[ $row4[ 'name' ] ] ++;
					$totals[ $row4[ 'name' ] ] += mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" );
				}
				else {
					if( !isset( $totals[ 'Independent' ] ) ) {
						$totals[ 'Independent' ] = 0;
						$count[ 'Independent' ] = 0;
					}
					$count[ 'Independent' ] ++;
					$totals[ 'Independent' ] += mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" );
				}
				
			    if( mysql_result( $result2, $p, "ruler_id" ) != -1 ) {
				    $query3 = "SELECT name " .
				    		"FROM ruler " .
				    		"WHERE ruler_id = " . mysql_result( $result2, $p, "ruler_id" );
				    $result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				    $row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );		    	
					$ruler_name = " - <a href=\"../overview/ruler.php?name=" . $row3[ 'name' ] . "\">$strong" . $row3[ 'name' ] . "$end</a>";
			    }
			    else {
			    	$ruler_name = ' - ' . $strong . translate( 'Independent forces' ) . $end;
			    }
				
				$direction = get_army_direction( mysql_result( $result2, $p, "army_id" ) );
				
				$last .= $army_name . $ruler_name . $faction_name . " - " .
						$strong . number_format( mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" ) ) . " - " . 
						$direction .
						$end . "<br />";
				$p ++;
			}
			$last .= "<br />\r\n";
		}
		if( !isset( $last ) ) {
			$last = "";
		}
		
		if( count( $totals ) > 0 ) {
			//$last .= "<br />\n";
			$show = false;
			foreach( $count AS $check ) {
				if( $check > 1 ) {
					$show = true;
				}
			}
			if( $show ) {
				foreach( $totals AS $key => $value ) {
					if( $key == 'Independents' ) {
						$last .= $colors[$key] . translate( $key ) . ' ' . translate( 'total forces' ) . ": " . number_format( $value ) . "</strong><br />\n";
					}
					else {
						$last .= $colors[$key] . $key . ' ' . translate( 'total forces' ) . ": " . number_format( $value ) . "</strong><br />\n";
					} 
				}
			}
		}
		
		$text .= $last;		
	}
	else {
		$text = translate( "Invalid location!" );
	}

	print( $text );
}
else {
	transprint( 'You must log in to get information from the map.' );
}
?>